package com.ruoyi.client.cloudcc.api.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.kevinsawicki.http.HttpRequest;
import com.ruoyi.client.cloudcc.api.UserApiService;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.client.cloudcc.constants.CloudCCConstants;
import com.ruoyi.client.cloudcc.utils.CloudCCAPIUtils;

/**
 * CloudCC用户处理接口
 */
@Service
public class UserApiServiceImpl implements UserApiService {


    @Autowired
    private RedisCache redisCache;

    //系统参数service
    @Autowired
    private ISysConfigService configService;

    @Override
    public String getAppUrl(String userName) {
        //获取应用地址
        String result = HttpRequest.post(configService.selectConfigByKey(CloudCCConstants.CloudCC_BASE_URL))
                .form("username",userName).body();
        String apiUrl = JSON.parseObject(result).getString("result");

        //获取系统设置
        SysConfig config = configService.checkConfigKeyUnique(CloudCCConstants.CloudCC_API_URL);
        if(StringUtils.isNotNull(config)){
            config.setConfigValue(apiUrl);
            configService.updateConfig(config);
        }else{
            config = new SysConfig();
            config.setConfigValue(apiUrl);
            config.setConfigName("CloudCC接口地址");
            config.setConfigType("Y");
            config.setConfigKey(CloudCCConstants.CloudCC_API_URL);
            configService.insertConfig(config);
        }

        return apiUrl;
    }

    @Override
    public String login(String userName, String password) {
        return login(userName,password,false);
    }

    @Override
    public String login(String userName, String password, boolean isMD5) {
        JSONObject result = CloudCCAPIUtils.createRequest(configService.selectConfigByKey(CloudCCConstants.CloudCC_API_URL), CloudCCConstants.SERVICENAME_CLOGIN)
                .appendParam("userName",userName)
                .appendParam("password",password)
                .appendParam("isMD5",isMD5)
                .send();
        String binding = result.getString("binding");
        return binding;
    }

    @Override
    public String getBinding(String userName, String authCode) {
        JSONObject result = CloudCCAPIUtils.createRequest(configService.selectConfigByKey(CloudCCConstants.CloudCC_API_URL),CloudCCConstants.SERVICENAME_CSSO_LOGIN)
                .appendParam("userName",userName)
                .appendParam("authCode",authCode)
                .send();
        String binding = result.getString("binding");
        return binding;
    }

    @Override
    public String getBinding() throws Exception {
        Object obj = redisCache.getCacheObject(CloudCCConstants.CloudCC_Binding);
        if(StringUtils.isNotNull(obj) && isValidWithBinding(obj.toString())){
            return obj.toString();
        }

        String binding = login(configService.selectConfigByKey(CloudCCConstants.CloudCC_USER_NAME),configService.selectConfigByKey(CloudCCConstants.CloudCC_PASSWORD));
        redisCache.setCacheObject(CloudCCConstants.CloudCC_Binding,binding);
        return binding;
    }

    @Override
    public boolean isValidWithBinding(String binding) throws Exception {
        JSONObject result = CloudCCAPIUtils.createRequest(configService.selectConfigByKey(CloudCCConstants.CloudCC_API_URL), CloudCCConstants.SERVICENAME_Valid_With_Binding)
                .appendParam("binding",binding)
                .send();

        if(!result.getBoolean("result")){
            throw new Exception(result.getString("returnInfo"));
        }

        return result.getJSONObject("data").getBoolean("isValid");
    }
}
